package network;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class URLConnectionEx extends JFrame {

	private JPanel contentPane;
	private JPanel panel;
	private JButton btnNewButton;
	private JTextField st;
	private JScrollPane scrollPane;
	private JTextArea result;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					URLConnectionEx frame = new URLConnectionEx();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */

	public void process() {

		String d = st.getText();
		
		if("http://".equals(d.substring(0,6))){
			
		}else{d= "http://"+d;}
			
				
//		if (!d.contains("http://")) {
//			d = "http://" + d;
//		} else if (!d.contains("www"))
//			d = "www." + d;

		try {
			URL url = new URL(d);
			result.append("호스트명:" + url.getHost() + "\n");
			result.append("포트:" + url.getPort());

			URLConnection uc = url.openConnection();
			result.append("mime type:" + uc.getContentType() + "\n");
			result.append("Enciding:" + uc.getContentEncoding() + "\n");
			result.append("Header:" + uc.getHeaderFields());
			result.append("--------------------------------\n");

			InputStream is = uc.getInputStream();
			InputStreamReader isr = new InputStreamReader(is, "utf-8");
			BufferedReader br = new BufferedReader(isr);

			String str = "";
			while (true) {
				str = br.readLine();
				if (str == null)
					break;

				result.append(str + "\n");
			}
			br.close();
			isr.close();
			is.close();

		} catch (Exception ex) {
			result.append("\n" + ex.toString() + "\n\n");
		}
	}

	public URLConnectionEx() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getScrollPane(), BorderLayout.CENTER);
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getBtnNewButton(), BorderLayout.EAST);
			panel.add(getSt(), BorderLayout.CENTER);
		}
		return panel;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("확인");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					process();

				}
			});
		}
		return btnNewButton;
	}

	private JTextField getSt() {
		if (st == null) {
			st = new JTextField();
			st.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_ENTER)
						process();
				}
			});
			st.setColumns(10);
		}
		return st;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}

	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}
}
